﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OAPlatform.CommonHelper;

namespace OAPlatform.DomainModel.Model
{
    /// <summary>
    /// 权限集
    /// 包含一组权限，并封装对这些权限的操作
    /// </summary>
    public class Role : BaseEntity
    {
        public Role()
        {
            Permissions = new List<Permission>();
        }

        /// <summary>
        /// 权限集合
        /// </summary>
        public virtual List<Permission> Permissions { get; set; }

        /// <summary>
        /// 添加一个权限到集合中
        /// </summary>
        /// <param name="permission"></param>
        public void Add(Permission permission)
        {
            Check.Argument.IsNotNull(permission, "权限");
            Permissions.RemoveAll(p => p.Key == permission.Key);
            Permissions.Add(permission);
        }

        /// <summary>
        /// 判断权限集合中是否有指定的权限
        /// </summary>
        /// <param name="permissionKey">指定权限的标识</param>
        /// <returns>存在则返回true，否则返回false</returns>
        public bool Authenticate(string permissionKey)
        {
            Check.Argument.IsNotNullOrEmpty(permissionKey, "权限键值");

            return Permissions.Exists(p => p.Key == permissionKey && !p.Dinied);
        }
    }
}
